package com.mazaiting.redeye.base

import cn.jpush.android.api.JPushInterface
import cn.jpush.android.api.JPushMessage
import cn.jpush.im.android.api.JMessageClient
import com.mazaiting.easy.app.BaseApplication
import com.mazaiting.easy.config.BaseConfig
import com.mazaiting.log.L
import com.mazaiting.redeye.BuildConfig
import com.mazaiting.redeye.base.module.ApplicationModule
import com.mazaiting.redeye.event.ImMessageEvent
import com.mazaiting.redeye.utils.SignUtil
import com.mob.MobSDK
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import com.umeng.socialize.PlatformConfig

/***
 *
 * @author mazaiting
 * @date 2019/5/30
 * @decription App 入口
 */
class MainApplication : BaseApplication() {

    /**
     * IM 消息事件接收器
     */
    private val mImMessageEvent: ImMessageEvent = ImMessageEvent()

    override fun initApp(): BaseApplication? = this

    override fun getConfig(): BaseConfig = Config()

    override fun initApplicationComponent() {
        super.initApplicationComponent()
        mApplicationComponent =
                DaggerApplicationComponentImpl
                    .builder()
                    .applicationModule(ApplicationModule(this))
                    .build()
    }

    override fun initOtherConfig() {
        super.initOtherConfig()
        initUmeng()
        initJPush()
        initMob()
//        L.d("Signature: " + SignUtil.getSignature(this))
    }

    /**
     * 初始化短信验证码服务
     */
    private fun initMob() {
        MobSDK.init(this)
    }

    /**
     * 初始化友盟
     */
    private fun initUmeng() {
        // 设置日志
        UMConfigure.setLogEnabled(BuildConfig.DEBUG)
        // 初始化
        UMConfigure.init(this, Config.UMENG_KEY, Config.UMENG, UMConfigure.DEVICE_TYPE_PHONE, null)
        // 采用页面自动采集
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
        // 设置分享平台
        PlatformConfig.setQQZone(Config.QQ_ID, Config.QQ_KEY)
    }

    /**
     * 初始化极光推送
     */
    private fun initJPush() {
        // 推送
        JPushInterface.setDebugMode(true)
        JPushInterface.init(this)
        // IM
        JMessageClient.setDebugMode(true)
        JMessageClient.init(this)
        // 注册事件接收器
        JMessageClient.registerEventReceiver(mImMessageEvent)
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        L.d("application-onTrimMemory")
//        // 取消注册
//        JMessageClient.unRegisterEventReceiver(mImMessageEvent)
//        // 退出登录
//        JMessageClient.logout()
    }

}
